package cn.test.controller;

import cn.test.pojo.Activity;
import cn.test.pojo.ActivityVo;
import cn.test.pojo.Type;
import cn.test.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import tk.mybatis.spring.annotation.MapperScan;

import java.util.List;

/**
 * @Author:ggj
 * @Date:2020/04/07/21:19
 * @Description:
 */

@Controller
@MapperScan("cn.test.mapper")
public class ActivityController {

    @Autowired
    ActivityService activityService;

    //跳转页面
    @GetMapping("act")
    public String index(){
        return "activity";
    }
    //跳转页面
    @GetMapping("addAct")
    public String add(){
        return "add";
    }

    //查询所有活动详情
    @GetMapping("/activity")
    public String queryAll(Model model){
        List<ActivityVo> activityVos = null;
        try {
            activityVos = activityService.queryAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("activities",activityVos);
        return "index";
    }

    //增加活动
    @PostMapping("/addActivity")
    public String addActivity(ActivityVo activityVo){
        //System.out.println(activityVo);
        activityService.addActivity(activityVo);
        return "redirect:activity";
    }

    //根据id删除活动
    @GetMapping("/delById/{id}")
    public String delById(@PathVariable Integer id){
        int i = activityService.delById(id);
        if (i>0){
            System.out.println("删除成功");
        }
        return "redirect:/activity";
    }

    //根据id查询活动
    @GetMapping("/findById/{id}")
    public String findById(@PathVariable Integer id,Model model){
        ActivityVo byId = activityService.findById(id);
        //System.out.println(byId);
        model.addAttribute("act",byId);
        return "update";
    }

    //修改活动
    @PostMapping("/updateActivity")
    public String updateActivity(ActivityVo activityVo){
        activityService.updateActivity(activityVo);
        return "redirect:/activity";
    }




}
